//
//  ConvenientlyTakeAddVC.m
//  ConvenientlyTake
//
//  Created by 张奇 on 2022/12/21.
//

#import "ConvenientlyTakeAddVC.h"
#import "TGMultilineTextField.h"
#import "TGChooseVideoView.h"
#import "EventTypeModel.h"
#import "ConvenientlyTakeAPIRequest.h"
#import "MapVC.h"

@interface ConvenientlyTakeAddVC () <TGChoosePhotoViewDelegate, TGChooseVideoViewDelegate, QMUITextViewDelegate, TGMultilineTextFieldDelegate, QMUITextFieldDelegate>
@property (weak, nonatomic) IBOutlet QMUITextView *descTextView;
@property (weak, nonatomic) IBOutlet UILabel *descNumLabel;

@property (weak, nonatomic) IBOutlet QMUITextField *phoneTF;
@property (weak, nonatomic) IBOutlet TGMultilineTextField *addressTF;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *addressTFHeight;

@property (weak, nonatomic) IBOutlet QMUITextField *typeTF;


@property (weak, nonatomic) IBOutlet UILabel *photoNumLabel;
@property (weak, nonatomic) IBOutlet TGChoosePhotoView *choosePhotoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *choosePhotoViewHeight;


@property (weak, nonatomic) IBOutlet UILabel *videoNumLabel;
@property (weak, nonatomic) IBOutlet TGChooseVideoView *chooseVideoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *chooseVideoViewHeight;

@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@property (nonatomic, strong) NSArray *imgUrlInfoArray, *videoUrlInfoArray;

@property (nonatomic, strong) NSMutableArray *eventTypeNameArray, *eventTypeIdArray;

@property (nonatomic, copy) NSString *selectEventTypeId;
@property (nonatomic, copy) NSString *selectProvince, *selectCity, *selectDiscrit;
@property (nonatomic, copy) NSString *selectLatitude, *selectLongitude;

@end

@implementation ConvenientlyTakeAddVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"随手拍";
    [self setupUI];
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    [self setNavStyleWhiteAndBackBtn];
}

- (void)setupUI {
    self.descTextView.placeholder = @"请在此输入处置描述";
    self.descTextView.placeholderColor = TextViewPlaceholderColor;
    self.descTextView.font = TGMediumAFont(15);
    self.descTextView.textColor = DeepBlackColor;
    self.descTextView.delegate = self;
    
    self.phoneTF.maximumTextLength = 11;
    self.addressTF.delegate = self;
    
    self.choosePhotoView.uploadImageType = UploadImageType_CONTRACT_SERVICE;
    self.choosePhotoView.maxCount = 10;
    self.choosePhotoViewHeight.constant = PhotoCollectionViewHeight(1);
    self.choosePhotoView.delegate = self;
    
    self.chooseVideoView.uploadVideoType = UploadImageType_CONTRACT_SERVICE;
    self.chooseVideoView.maxCount = 10;
    self.chooseVideoViewHeight.constant = PhotoCollectionViewHeight(1);
    self.chooseVideoView.delegate = self;
    
    self.submitBtn.layer.cornerRadius = TableViewCornerRadius;
    self.submitBtn.layer.masksToBounds = YES;
}

- (IBAction)clickChooseAddress:(UIButton *)sender {
    MapVC *vc = [[MapVC alloc] init];
    [self pushViewController:vc complent:nil];
    WEAK
    [vc setClickclickconfirmResultsBlock:^(MapResultsModel * _Nonnull model) {
        NSLog(@"地址==%@", model);
        STRONG
        NSMutableArray *addressArray = [NSMutableArray array];
        if (!EmptyCheck(model.province)) {
            [addressArray addObject:model.province];
        }
        if (!EmptyCheck(model.city) && ![model.province isEqualToString:model.city]) {
            [addressArray addObject:model.city];
        }
        if (!EmptyCheck(model.district)) {
            [addressArray addObject:model.district];
        }
//        self.addressTF.text = [addressArray componentsJoinedByString:@"·"];
        self.addressTF.text = [NSString stringWithFormat:@"%@%@%@", [addressArray componentsJoinedByString:@""],model.address, model.name];
        self.selectProvince = model.pcode;
        self.selectCity = model.citycode;
        self.selectDiscrit = model.adcode;
        self.selectLatitude = String(model.latitude);
        self.selectLongitude = String(model.longitude);
    }];
}

- (IBAction)clickChooseType:(UIButton *)sender {

    if (self.eventTypeNameArray.count > 0) {
        [TGLEEAlert actionSheetTitle:@"" buttons:self.eventTypeNameArray isHaveCancleAction:YES canClickBackgroundClose:YES click:^(NSInteger index) {
            
            self.typeTF.text = self.eventTypeNameArray[index];
            self.selectEventTypeId = self.eventTypeIdArray[index];
            
        }];
    } else {
        [self requestEventTypeData];
    }
 
}

- (void)requestEventTypeData {
    
    [ConvenientlyTakeAPIRequest GETEventTypeWithKeyword:@"" success:^(TGResponseItem *responseItem) {
        if (responseItem.success) {
            
            NSArray *list = [NSArray modelArrayWithClass:[EventTypeModel class] json:responseItem.data];
            self.eventTypeNameArray = [NSMutableArray array];
            self.eventTypeIdArray = [NSMutableArray array];
            for (EventTypeModel *typeModel in list) {
                [self.eventTypeNameArray addObject:R(typeModel.eventTypeName)];
                [self.eventTypeIdArray addObject:R(typeModel.eventTypeId)];
            }
            
            [TGLEEAlert actionSheetTitle:@"" buttons:self.eventTypeNameArray isHaveCancleAction:YES canClickBackgroundClose:YES click:^(NSInteger index) {
                
                self.typeTF.text = self.eventTypeNameArray[index];
                self.selectEventTypeId = self.eventTypeIdArray[index];
                
            }];
            
        }
        
    } fail:^(NSError *error) {
        
    }];
}


- (IBAction)clickSubmit:(UIButton *)sender {
    if (![self checkoutUserInput]) {return;}
    
    NSMutableArray *imagesList = [NSMutableArray array];
    for (TGUrlInfoModel *imgInfoModel in self.imgUrlInfoArray) {
        [imagesList addObject:@{@"fileType": @"1", @"fileName": R(imgInfoModel.filename), @"filePath": R(imgInfoModel.fullPath)}];
    }
    
    NSMutableArray *videoList = [NSMutableArray array];
    for (TGUrlInfoModel *videoInfoModel in self.videoUrlInfoArray) {
        [videoList addObject:@{@"fileType": @"2", @"fileName": R(videoInfoModel.filename), @"filePath": R(videoInfoModel.fullPath)}];
    }
    
    [TGProgressHUD showHUD:nil];
    [ConvenientlyTakeAPIRequest POSTConvenientTakeAddWithProblemDescription:self.descTextView.text promoterPhone:self.phoneTF.text province:self.selectProvince city:self.selectCity county:self.selectDiscrit address:self.addressTF.text longitude:self.selectLongitude latitude:self.selectLatitude eventType:self.selectEventTypeId imagesList:imagesList videoList:videoList success:^(TGResponseItem *responseItem) {
        [TGProgressHUD dismissMPHUD];
        if (responseItem.success) {
            [TGProgressHUD showHUDSucessTip:@"提交成功!"];
            [TGGCD executeAfterTime:1 task:^{
                [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationWithRefresheMainteanceReportListVC object:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
    } fail:^(NSError *error) {
        
    }];
}


#pragma mark QMUITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length > 200) {
        [TGProgressHUD showHUDTip:@"处置描述不能超过200个字符"];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:@"200/200"];
        [attStr addAttribute:NSForegroundColorAttributeName value:UIColorRed range:NSMakeRange(0, 3)];
        self.descNumLabel.attributedText = attStr;
        self.descTextView.text = [textView.text substringToIndex:200];
    } else {
        self.descNumLabel.text = [NSString stringWithFormat:@"%zd/200", textView.text.length];
    }
}

#pragma mark TGMultilineTextFieldDelegate
- (void)multilineTextField:(TGMultilineTextField *)multilineTextField newHeightAfterTextChanged:(CGFloat)height {
    self.addressTFHeight.constant = height;
}

#pragma mark TGChoosePhotoViewDelegate
- (void)tgChoosePhotoViewDidSelectImgArray:(NSArray *)imgArray imgUrlInfoArray:(NSArray <TGUrlInfoModel *>*)imgUrlInfoArray {
    NSLog(@"++++%@,-----%@", imgArray, imgUrlInfoArray);
    NSInteger count = ceil((imgArray.count+1)/4.0);
    self.choosePhotoViewHeight.constant = PhotoCollectionViewHeight(count);
    self.imgUrlInfoArray = imgUrlInfoArray;
    self.photoNumLabel.text = [NSString stringWithFormat:@"照片(%ld/10)", imgUrlInfoArray.count];
}

#pragma mark TGChooseVideoViewDelegate
- (void)tgChooseVideoViewDidSelectvideoUrlInfoArray:(NSArray <TGUrlInfoModel *>* _Nonnull)videoUrlInfoArray {
    NSLog(@"++++-----%@", videoUrlInfoArray);
    NSInteger count = ceil((videoUrlInfoArray.count+1)/4.0);
    self.chooseVideoViewHeight.constant = PhotoCollectionViewHeight(count);
    self.videoUrlInfoArray = videoUrlInfoArray;
    self.videoNumLabel.text = [NSString stringWithFormat:@"视频(%ld/10)", videoUrlInfoArray.count];
}

- (BOOL)checkoutUserInput {
    if (EmptyCheck(self.descTextView.text)) {
        [TGProgressHUD showHUDTip:@"请输入处置描述"];
        return NO;
    }
    if (EmptyCheck(self.phoneTF.text)) {
        [TGProgressHUD showHUDTip:@"请输入联系方式"];
        return NO;
    }
    if (self.phoneTF.text.length != 11) {
        [TGProgressHUD showHUDTip:@"请输入正确的联系方式"];
        return NO;
    }
    if (EmptyCheck(self.addressTF.text)) {
        [TGProgressHUD showHUDTip:@"请选择发生地点"];
        return NO;
    }
    if (EmptyCheck(self.selectEventTypeId)) {
        [TGProgressHUD showHUDTip:@"请选择事件类型"];
        return NO;
    }
    
    
    return YES;
}

@end
